<template>
  <div class="app-container">
    <div class="filter-container">
      <el-breadcrumb separator="/" class="breadcrumb-container">
        <el-breadcrumb-item :to="{ path: '/forum/users' }">用户列表</el-breadcrumb-item>
        <el-breadcrumb-item>用户帖子</el-breadcrumb-item>
      </el-breadcrumb>
      
      <div style="margin: 20px 0;">
        <h3>用户: {{ userName }}</h3>
      </div>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="标题">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="分类">
        <template slot-scope="scope">
          <el-tag>{{ getCategoryName(scope.row.categoryId) }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column width="80px" align="center" label="浏览">
        <template slot-scope="scope">
          <span>{{ scope.row.viewCount }}</span>
        </template>
      </el-table-column>

      <el-table-column width="80px" align="center" label="点赞">
        <template slot-scope="scope">
          <span>{{ scope.row.likeCount }}</span>
        </template>
      </el-table-column>

      <el-table-column width="80px" align="center" label="评论">
        <template slot-scope="scope">
          <span>{{ scope.row.commentCount }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="发布时间">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.createTime).getTime() | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleViewComments(scope.row)"
          >
            查看评论
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <div v-if="list.length === 0 && !listLoading" class="empty-text">
      该用户暂无帖子
    </div>
  </div>
</template>

<script>
import { getUserPosts, deletePost } from '@/api/post'
import { getCategories } from '@/api/category'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'

export default {
  name: 'UserPosts',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      userId: null,
      userName: '未知用户',
      categoryOptions: []
    }
  },
  created() {
    this.userId = this.$route.params.userId
    this.getUserName()
    this.getCategories()
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const response = await getUserPosts(this.userId, this.listQuery)
        this.list = response.data.records
        this.total = response.data.total
      } catch (error) {
        console.error('获取用户帖子列表失败:', error)
        this.list = []
      } finally {
        this.listLoading = false
      }
    },
    async getCategories() {
      try {
        const response = await getCategories()
        this.categoryOptions = response.data
      } catch (error) {
        console.error('获取分类列表失败:', error)
      }
    },
    getUserName() {
      // 这里可以从路由参数或store中获取用户名
      // 暂时使用用户ID作为显示
      this.userName = `用户 ${this.userId}`
    },
    getCategoryName(categoryId) {
      const category = this.categoryOptions.find(item => item.id === categoryId)
      return category ? category.name : '未知'
    },
    handleViewComments(row) {
      this.$router.push(`/forum/comments/${row.id}`)
    },
    handleDelete(row) {
      this.$confirm('确定要删除这个帖子吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deletePost(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        } catch (error) {
          console.error('删除帖子失败:', error)
        }
      })
    }
  }
}
</script>

<style scoped>
.breadcrumb-container {
  margin-bottom: 20px;
}

.empty-text {
  text-align: center;
  padding: 40px;
  color: #909399;
  font-size: 14px;
}
</style>